﻿using System;
using System.IO;
using Microsoft.WowAddonStudio.Project.Import;
using NUnit.Framework;

namespace WowProjectPackage.Tests.Import
{
	/// <summary>
	/// Tests for addon item copy mechanism.
	/// </summary>
	[TestFixture]
	public class AddonItemCopierTest
	{
		private const string testDirectory = "c:\\WowStudioTests";
		private const string testTargetDirectory = "c:\\WowStudioTests\\Target";
		private const string testNotExistingDirectory = "c:\\WowStudioTests\\NotExisting";
		private const string testFile = "test.txt";

		private string sourceAbsolutePath;
		private string targetAbsolutePath;

		[SetUp]
		public void SetUp()
		{
			sourceAbsolutePath = Path.Combine(testDirectory, testFile);
			targetAbsolutePath = Path.Combine(testTargetDirectory, testFile);

			if (!Directory.Exists(testDirectory))
				Directory.CreateDirectory(testDirectory);

			if (!File.Exists(sourceAbsolutePath))
			{
				using (FileStream fs = File.Create(sourceAbsolutePath))
				{
					fs.Close();
				}
			}

			if (!Directory.Exists(testTargetDirectory))
				Directory.CreateDirectory(testTargetDirectory);
			else if (File.Exists(targetAbsolutePath))
				File.Delete(targetAbsolutePath);

			if (Directory.Exists(testNotExistingDirectory))
				Directory.Delete(testNotExistingDirectory, true);
		}

		[TearDown]
		public void TearDown()
		{
			//TODO: Cleanup...
		}

		[Test]
		[ExpectedException(typeof (ArgumentNullException))]
		public void TestNullTargetPath()
		{
			AddonItemCopier.CopyAddonItem(null, null);
		}

		[Test]
		[ExpectedException(typeof (ArgumentNullException))]
		public void TestEmptyTargetPath()
		{
			AddonItemCopier.CopyAddonItem(string.Empty, null);
		}

		[Test]
		[ExpectedException(typeof (ArgumentException),
			ExpectedMessage = "Target path is not a valid folder name.", MatchType = MessageMatch.Contains)]
		public void TestInvalidTargetPath()
		{
			AddonItemCopier.CopyAddonItem("?dfjfdkj '' <$x", new FolderAddonItem(testDirectory, testFile));
		}

		[Test]
		[ExpectedException(typeof (ArgumentNullException))]
		public void TestUnspecifiedAddonItem()
		{
			AddonItemCopier.CopyAddonItem("d:\\Temp", null);
		}

		[Test]
		[ExpectedException(typeof (IOException))]
		public void TestOverwriteFailure()
		{
			//To prevent accidental file overwrites...
			AddonItemCopier.CopyAddonItem(testDirectory, new FolderAddonItem(testDirectory, testFile));
		}

		[Test]
		public void TestCopyToExistingFolder()
		{
			AddonItemCopier.CopyAddonItem(testTargetDirectory, new FolderAddonItem(testDirectory, testFile));
			Assert.IsTrue(File.Exists(targetAbsolutePath));
		}

		[Test]
		public void TestCopyToNotExistingFolder()
		{
			AddonItemCopier.CopyAddonItem(testNotExistingDirectory, new FolderAddonItem(testDirectory, testFile));
			Assert.IsTrue(File.Exists(Path.Combine(testNotExistingDirectory, testFile)));
		}

		//[Test]
		//public void TestCopyLargeFile()
		//{
		//    using (FileStream fs = File.Open(sourceAbsolutePath, FileMode.Append, FileAccess.Write))
		//    {

		//        fs.Close();
		//    }
		//}
	}
}